package com.color.admin.common.core.page;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * @author HaiQiu
 * @date 2021/11/18
 * 分页工具
 */
public class Page {

    /**
     * 默认页数
     */
    private static final int DEFAULT_INDEX = 1;
    /**
     * 默认每页大小
     */
    private static final int DEFAULT_SIZE = 20;
    /**
     * 默认最大每页大小
     */
    private static final int DEFAULT_SIZE_MAX = 1000;

    /**
     * 页数
     */
    protected int pageIndex;
    /**
     * 每页大小
     */
    protected int pageSize;

    /**
     * 排序字段
     */
    protected String prop;

    /**
     * 排序模式：desc asc
     */
    protected String order;

    Page() {
        this.pageIndex = DEFAULT_INDEX;
        this.pageSize = DEFAULT_SIZE;
    }

    Page(int pageIndex, int pageSize) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    /**
     * 数据库查询分页偏移量
     *
     * @return 分页偏移量
     */
    @JsonIgnore
    public int getOffset() {
        return (getPageIndex() - 1) * pageSize;
    }


    /**
     * 数据库Jpa查询分页偏移量
     *
     * @return Jpa分页偏移量
     */
    @JsonIgnore
    public int getNum() {
        return getPageIndex() - 1;
    }


    public int getPageIndex() {
        if (pageIndex <= 0) {
            return this.pageIndex = DEFAULT_INDEX;
        } else {
            return this.pageIndex;
        }
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize == 0 || pageSize<= 0L || pageSize >= DEFAULT_SIZE_MAX){
            this.pageSize = DEFAULT_SIZE;
        }else {
            this.pageSize = pageSize;
        }
    }


    public String getProp() {
        return prop;
    }

    public void setProp(String prop) {
        this.prop = prop;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }
}
